A parameter is an item of data supplied to a method or a constructor.
toString()
Method
The documentation shows toString()
with no parameters inside the
parentheses.
This means that no data is supplied to the method when you call it.
However, the parentheses must still be there.
Here is the example program:
import java.awt.*; class PointEg2 { public static void main ( String arg[] ) { Point a, b, c; // reference variables a = new Point(); // create a Point at ( 0, 0); // save the reference in "a" b = new Point( 12, 45 ); // create a Point at (12, 45); // save the reference in "b" c = new Point( b ); // create a Point String strA = a.toString(); // create a String object based on the data // found in the object referenced by "a". System.out.println( strA ); } }
When this program runs, the statement:
String strA = a.toString(); // create a String object based on the data
creates a String
object based on the data in the object referred to by a
.
The reference variable strA
keeps track of this new String
object.
Then the characters from the String
are sent to the monitor with println
.
The program prints out:
java.awt.Point[x=0,y=0]
The Point
object has not been altered: it still exists and is referred to by a
.
Its method toString()
created the characters that were printed out and
stored them in a String
object.
(Of course,
you will be eager to copy this program into Notepad and to compile and run it.
Then you will almost certainly modify the program to
use the toString()
method with the
other two objects as well.)
Just as the program is about to end, how many objects are there and how many object references are there? Has any garbage been created?